package com.where_to_play;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.where_to_play.R;
import com.where_to_play.adapter.PagerAdapter_Guide_activity;

public class Guide_Activity extends Activity {
	private ViewPager viewpager_guide;
	private RadioGroup radiogroup_guide;
	private RadioButton[] radiobutton_guide;
	private List<View> listtotal_guide = new ArrayList<View>();
	private PagerAdapter_Guide_activity adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		getActionBar().hide();
		initData();
		initView();
		initDots();
	}

	private void initDots() {
		// TODO Auto-generated method stub
		radiobutton_guide = new RadioButton[listtotal_guide.size()];
		for (int i = 0; i < listtotal_guide.size(); i++) {
			RadioButton radioButton = new RadioButton(this);
			radioButton.setButtonDrawable(R.drawable.bg_radiobutton_guide);
			radiobutton_guide[i] = radioButton;
			radiogroup_guide.addView(radioButton);

		}
		radiobutton_guide[0].setChecked(true);
		radiogroup_guide
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						for (int i = 0; i < radiobutton_guide.length; i++) {
							if (radiobutton_guide[i].getId() == checkedId) {
								viewpager_guide.setCurrentItem(i);
							}
						}
					}
				});
		viewpager_guide.setClickable(true);

		radiobutton_guide[radiobutton_guide.length - 1]
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(Guide_Activity.this,
								MainActivity.class);
						startActivity(intent);
					}
				});

	}

	private void initView() {
		// TODO Auto-generated method stub
		viewpager_guide = (ViewPager) findViewById(R.id.viewPager_guide);
		radiogroup_guide = (RadioGroup) findViewById(R.id.radioGroup_guide);
		adapter = new PagerAdapter_Guide_activity(listtotal_guide);
		viewpager_guide.setAdapter(adapter);
		viewpager_guide.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				radiobutton_guide[arg0].setChecked(true);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void initData() {
		// TODO Auto-generated method stub
		TypedArray arr = getResources().obtainTypedArray(R.array.img_arr_guide);
		for (int i = 0; i < arr.length(); i++) {
			ImageView imageView = new ImageView(this);
			imageView.setScaleType(ScaleType.FIT_XY);
			imageView.setImageDrawable(arr.getDrawable(i));
			listtotal_guide.add(imageView);
			if (i != arr.length() - 1)
				continue;
			imageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(Guide_Activity.this,
							MainActivity.class);
					startActivity(intent);
				}
			});
		}
	}

}
